unit uFrmMsgBox;

interface

uses
  Windows, Controls, StdCtrls, ExtCtrls, Classes, Forms, pngimage,
  cxGraphics,
  cxLookAndFeels, cxLookAndFeelPainters, Menus, cxButtons;

type
  TFrmMsgBox = class(TForm)
    pnlClient: TPanel;
    img1: TImage;
    lblText: TLabel;
    pnlBottom: TPanel;
    btnOk: TcxButton;
    btnCancel: TcxButton;
    bvl1: TBevel;
  private
    procedure LoadIcon(AFlag: Integer);
    procedure LoadResStream(AImg: TImage; const ResName: string);
    procedure InitMsgForm(const AText, ACaption: string; AFlag: Integer);
  public
    class function ShowDialogForm(const AText, ACaption: string;
      AFlag: Integer): Boolean;
  end;

var
  FrmMsgBox: TFrmMsgBox;

implementation

{$R *.dfm}

{ TFrmMsg }

procedure TFrmMsgBox.LoadResStream(AImg: TImage; const ResName: string);
var
  ResStream: TResourceStream;
  png: TPNGObject;
begin
  ResStream := TResourceStream.Create(HInstance, ResName, RT_RCDATA);
  png := TPNGObject.Create;
  try
    ResStream.Position := 0;
    png.LoadFromStream(ResStream);
    AImg.Picture.Assign(png);
  finally
    png.Free;
    ResStream.Free;
  end;
end;

procedure TFrmMsgBox.LoadIcon(AFlag: Integer);
var
  lResName: string;
begin
  case AFlag of
    16: lResName := 'ERROR';
    33: lResName := 'QUESTION';
    48: lResName := 'WARNING';
    64: lResName := 'INFORMATION';
    70: lResName := 'SUCCESS';
  else
    lResName := 'INFORMATION';
  end;
  LoadResStream(img1, lResName);
end;

procedure TFrmMsgBox.InitMsgForm(const AText, ACaption: string; AFlag: Integer);
begin
  Self.Caption := ACaption;
  lblText.Caption := AText;
  if not (AFlag in [33]) then
  begin
    btnCancel.Visible := False;
    btnOk.Left := btnCancel.Left;
  end;
  LoadIcon(AFlag);
end;

class function TFrmMsgBox.ShowDialogForm(const AText, ACaption: string;
  AFlag: Integer): Boolean;
var
  lForm: TFrmMsgBox;
  lMsgEvent: TMessageEvent;
begin
  lForm := TFrmMsgBox.Create(nil);
  lMsgEvent := Application.OnMessage;
  try
    Application.OnMessage := nil;
    lForm.InitMsgForm(AText, ACaption, AFlag);
    Result := lForm.ShowModal = mrOk;
  finally
    Application.OnMessage := lMsgEvent;
    lForm.Free;
  end;
end;

end.
